#pragma once

#include <iostream>
#include <cstdarg>
#include <ctime>

const int DEBUG =   0;
const int NORMAL =  1;
const int WARNING = 2;
const int ERROR =   3;
const int FATAL =   4;

const int STD_SIZE = 	0x400;
const int LOG_SIZE = 	0x400;
const int TIME_SIZE = 	0x40;

const std::string gLevelMap[] = {
    "DEBUG",
    "NORMA",
    "WARNI",
    "ERROR",
    "FATAL"
};

const std::string logFile = "./file.log";

void logMessage(const int level, const char* format, ...)
{
#ifndef DEBUG_SHOW
    if (DEBUG == level)  return ;
#endif
    char stdBuffer[STD_SIZE];
    char logBuffer[LOG_SIZE];
    char formatTime[TIME_SIZE];
    time_t t = time(nullptr);
    struct tm *timeinfo = localtime(&t);
    strftime(formatTime, sizeof formatTime, "%Y-%m-%d %H:%M:%S", timeinfo);
    snprintf(stdBuffer, sizeof stdBuffer, "[%s][%s] ", gLevelMap[level].c_str(), formatTime);
    va_list aq;
    va_start(aq, format);
    vsnprintf(logBuffer, sizeof logBuffer, format, aq);
    va_end(aq);
#ifdef LOG_FILE
    auto fp = fopen(logFile.c_str(), "a");
    fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
    fclose(fp);
#else
    printf("%s%s\n", stdBuffer, logBuffer);
#endif
}
